//
//  XLRegisterView.m
//  industry
//
//  Created by xl on 2020/11/20.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLRegisterView.h"
#import "XLVerificationCodeButton.h"
#import "XLLoginRegisterInteractorProtocol.h"
#import "XLLoginRegisterVC.h"
#import "XLLoginRegisterPresenterProtocol.h"
#import <YYText/YYText.h>
#import "NSString+XL.h"
#import "XLWebManager.h"

@interface XLRegisterView ()<XLVerificationCodeButtonDelegate>

@property (nonatomic, strong) XLButton *backBtn;
@property (nonatomic, strong) XLLabel *titleLB;
@property (nonatomic, strong) XLLabel *adLB;
@property (nonatomic, strong) XLImageView *circleBgIG;
@property (nonatomic, strong) XLView *adLeftLineView;
@property (nonatomic, strong) XLView *adRightLineView;
@property (nonatomic, strong) XLLabel *accountLB;
@property (nonatomic, strong) XLTextField *accountTF;
@property (nonatomic, strong) XLView *accountLineView;
@property (nonatomic, strong) XLLabel *passwordLB;
@property (nonatomic, strong) XLTextField *passwordTF;
@property (nonatomic, strong) XLView *passwordLineView;
@property (nonatomic, strong) XLButton *nextBtn;
@property (nonatomic, strong) XLVerificationCodeButton *vfCodeBtn;
@property (nonatomic, strong) XLLabel *checkLB;
@property (nonatomic, strong) XLTextField *checkTF;
@property (nonatomic, strong) XLImageView *checkIG;
@property (nonatomic, strong) XLView *checkLineView;

@property (nonatomic, strong) XLButton *selectBtn;
@property (nonatomic, strong) YYLabel *protocolLB;

@end
@implementation XLRegisterView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteBgColor];
        UIBezierPath *cornerRaduisPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(50, 50)];
        CAShapeLayer *cornerRadiusLayer = [[CAShapeLayer alloc] init];
        cornerRadiusLayer.frame = self.bounds;
        cornerRadiusLayer.path = cornerRaduisPath.CGPath;
        self.layer.mask = cornerRadiusLayer;
        
        [self addSubview:self.backBtn];
        [self addSubview:self.titleLB];
        [self addSubview:self.circleBgIG];
        [self addSubview:self.adLB];
        [self addSubview:self.adLeftLineView];
        [self addSubview:self.adRightLineView];
        
        [self addSubview:self.accountLB];
        [self addSubview:self.accountTF];
        [self addSubview:self.accountLineView];
        [self addSubview:self.passwordLB];
        [self addSubview:self.passwordTF];
        [self addSubview:self.passwordLineView];
        [self addSubview:self.vfCodeBtn];
        [self addSubview:self.checkLB];
        [self addSubview:self.checkTF];
        [self addSubview:self.checkIG];
        [self addSubview:self.checkLineView];
        [self addSubview:self.selectBtn];
        [self addSubview:self.protocolLB];
        [self addSubview:self.nextBtn];

        [self makeConstraints];
    }
    return self;
}

#pragma mark - Public
- (void)refreshVerificationCodeButtonState:(BOOL)state {
    if (state) {//获取校验码成功
        [_vfCodeBtn starCountTime];
    } else {//获取校验码失败
        [_vfCodeBtn lockVerificationCodeEnable:YES];
    }
}

- (void)refreshCheckImage {
    NSString *url = [NSString stringWithFormat:@"%@%@", XLServerHttpDomain, XLFetchImageVerifyUrl];
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:XLWebIMAGEURL(url)]];
    _checkIG.image = image;
}

#pragma mark - UIButton 点击事件
- (void)clickGoBack {
    [self.baseController.navigationController popViewControllerAnimated:YES];
}

//选中协议
- (void)clickSelectProtocol:(XLButton *)sender {
    sender.selected = !sender.selected;
}

//查看协议
- (void)clickWatchProtocol {
    [XLWebManager requestOPenWebUrl:[NSString stringWithFormat:@"%@%@", XLServerHttpDomain, XLAgreenDelegateWebUrl] title:@"服务条款和隐私政策"];
}

- (void)clickNext {
    if (!_selectBtn.isSelected) {
        [XLToast showComonMessage:@"注册前，请您先阅读《服务条款和隐私政策》（下称“协议”），协议内容中以加粗方式显著标识的条款，请您着重阅读。您勾选了“已阅读并同意《服务条款和隐私政策》”，点击“注册”按钮即视为您完全理解并接受该协议，同意按照协议条款履行。若存在您不理解或不同意的内容，请不要继续注册本平台。"];
        return;
    }
    
    if (ISEmptyString(_accountTF.text)) {
        [XLToast showComonMessage:@"请填写手机号码"];
        return;
    }
    
    if (ISEmptyString(_passwordTF.text)) {
        [XLToast showComonMessage:@"请填写短信验证码"];
        return;
    }
    
    if (ISEmptyString(_checkTF.text)) {
        [XLToast showComonMessage:@"请填写验证码"];
        return;
    }
    
    XLLoginRegisterVC *vc = (XLLoginRegisterVC *)self.baseController;
    XLSend(self.baseController.presenter, XLLoginRegisterPresenterProtocol, requestCheckRegisterOrForgetNextPhone:_accountTF.text numberType:2 verification:_passwordTF.text isStaff:vc.isStaff);
}

- (void)clickFetchImageVerify {
    [self refreshCheckImage];
}

#pragma mark - XLVerificationCodeButton
- (void)willGetVerificationCode {
    if (ISEmptyString(_accountTF.text)) {
        [XLToast showComonMessage:@"手机号不能为空"];
        return;
    }
    if (ISEmptyString(_checkTF.text)) {
        [XLToast showComonMessage:@"请先填写校验码"];
        return;
    }
    //获取登录校验码
    XLSend(self.baseController.presenter, XLLoginRegisterPresenterProtocol, requestCheckImageCode:_checkTF.text phone:_accountTF.text numberType:2);
}

#pragma mark - Private
- (void)makeConstraints {
    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.top.equalTo(self).offset(30);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    
    [_titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-20);
        make.centerY.equalTo(self.backBtn);
    }];
    [_titleLB sizeToFit];
    
    [_circleBgIG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(216, 188));
    }];
    
    [_adLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self).offset(-SAFEAREA_BOTTOM_HEIGHT - 15);
    }];
    [_adLB sizeToFit];
    
    [_adLeftLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.adLB);
        make.right.equalTo(self.adLB.mas_left).offset(-10);
        make.left.equalTo(self).offset(26);
        make.height.mas_equalTo(1);
    }];
    
    [_adRightLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.adLB);
        make.left.equalTo(self.adLB.mas_right).offset(10);
        make.right.equalTo(self).offset(-26);
        make.height.mas_equalTo(1);
    }];
    
    [_accountLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.top.equalTo(self).offset(70);
    }];
    [_accountLB sizeToFit];
    
    [_accountTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.accountLB);
        make.top.equalTo(self.accountLB.mas_bottom).offset(3);
        make.right.equalTo(self).offset(-30);
        make.height.mas_equalTo(40);
    }];
    
    [_accountLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.accountTF);
        make.top.equalTo(self.accountTF.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    
    [_passwordLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.accountLB);
        make.top.equalTo(self.accountLineView.mas_bottom).offset(5);
    }];
    
    [_passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.passwordLB);
        make.top.equalTo(self.passwordLB.mas_bottom).offset(3);
        make.right.equalTo(self).offset(-30 - 95);
        make.height.mas_equalTo(40);
    }];
    
    [_passwordLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.right.equalTo(self).offset(-30);
        make.top.equalTo(self.passwordTF.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    
    [_vfCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-36);
        make.centerY.equalTo(self.passwordTF);
        make.size.mas_equalTo(CGSizeMake(90, 32));
    }];
    
    [_checkLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.top.equalTo(self.passwordLineView.mas_bottom).offset(5);
    }];
    [_checkLB sizeToFit];
    
    [_checkTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.checkLB);
        make.top.equalTo(self.checkLB.mas_bottom);
        make.height.mas_equalTo(40);
        make.right.equalTo(self).offset(-95 - 30);
    }];
    
    [_checkIG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-30);
        make.centerY.equalTo(self.checkTF);
        make.size.mas_equalTo(CGSizeMake(90, 30));
    }];
    
    [_checkLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.right.equalTo(self).offset(-30);
        make.top.equalTo(self.checkTF.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    
    [_selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.checkLB);
        make.top.equalTo(self.checkLineView.mas_bottom).offset(17);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    
    [_protocolLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.selectBtn.mas_right).offset(10);
        make.centerY.equalTo(self.selectBtn);
    }];
    
    [_nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.right.equalTo(self).offset(-30);
        make.top.equalTo(self.protocolLB.mas_bottom).offset(15);
        make.height.mas_equalTo(45);
    }];
}

#pragma mark - setter/getter
- (XLButton *)backBtn {
    if (!_backBtn) {
        _backBtn = [XLButton new];
        [_backBtn setImage:XLIMAGE(@"back") forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(clickGoBack) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (XLLabel *)titleLB {
    if (!_titleLB) {
        _titleLB = [XLLabel new];
        _titleLB.font = [UIFont fontOfPingFangSCSemiboldWithSize:14];
        _titleLB.textColor = [UIColor blackFontColor];
        _titleLB.text = @"";
    }
    return _titleLB;
}

- (XLLabel *)adLB {
    if (!_adLB) {
        _adLB = [XLLabel new];
        _adLB.font = [UIFont fontOfPingFangSCRegularWithSize:11];
        _adLB.textColor = [UIColor grayFontColor];
        _adLB.text = @"为产业而金融，因科技而不同";
    }
    return _adLB;
}

- (XLView *)adLeftLineView {
    if (!_adLeftLineView) {
        _adLeftLineView = [XLView new];
        _adLeftLineView.backgroundColor = [UIColor grayLineColor];
    }
    return _adLeftLineView;
}

- (XLView *)adRightLineView {
    if (!_adRightLineView) {
        _adRightLineView = [XLView new];
        _adRightLineView.backgroundColor = [UIColor grayLineColor];
    }
    return _adRightLineView;
}

- (XLLabel *)accountLB {
    if (!_accountLB) {
        _accountLB = [XLLabel new];
        _accountLB.font = [UIFont fontOfPingFangSCSemiboldWithSize:15];
        _accountLB.textColor = [UIColor blackFontColor];
        _accountLB.text = @"手机号";
    }
    return _accountLB;
}

- (XLTextField *)accountTF {
    if (!_accountTF) {
        _accountTF = [XLTextField new];
        _accountTF.font = [UIFont fontOfPingFangSCRegularWithSize:16];
        _accountTF.placeholder = @"请输入您的手机号";
        _accountTF.tintColor = [UIColor redFontColor];
        _accountTF.textColor = [UIColor blackFontColor];
        _accountTF.keyboardType = UIKeyboardTypeNumberPad;
        _accountTF.enablesReturnKeyAutomatically = YES;
        _accountTF.returnKeyType = UIReturnKeyNext;
    }
    return _accountTF;
}

- (XLView *)accountLineView {
    if (!_accountLineView) {
        _accountLineView = [XLView new];
        _accountLineView.backgroundColor = [UIColor grayLineColor];
    }
    return _accountLineView;
}

- (XLLabel *)passwordLB {
    if (!_passwordLB) {
        _passwordLB = [XLLabel new];
        _passwordLB.font = [UIFont fontOfPingFangSCSemiboldWithSize:15];
        _passwordLB.textColor = [UIColor blackFontColor];
        _passwordLB.text = @"短信验证码";
    }
    return _passwordLB;
}

- (XLTextField *)passwordTF {
    if (!_passwordTF) {
        _passwordTF = [XLTextField new];
        _passwordTF.font = [UIFont fontOfPingFangSCRegularWithSize:16];
        _passwordTF.placeholder = @"请输入您的验证码";
        _passwordTF.tintColor = [UIColor redFontColor];
        _passwordTF.textColor = [UIColor blackFontColor];
        _passwordTF.enablesReturnKeyAutomatically = YES;
        _passwordTF.returnKeyType = UIReturnKeyNext;
    }
    return _passwordTF;
}

- (XLView *)passwordLineView {
    if (!_passwordLineView) {
        _passwordLineView = [XLView new];
        _passwordLineView.backgroundColor = [UIColor grayLineColor];
    }
    return _passwordLineView;
}

- (XLButton *)nextBtn {
    if (!_nextBtn) {
        _nextBtn = [XLButton new];
        _nextBtn.layer.cornerRadius = 22.5f;
        _nextBtn.layer.masksToBounds = YES;
        _nextBtn.backgroundColor = [UIColor blueBgColor];
        _nextBtn.titleLabel.font = [UIFont fontOfPingFangSCSemiboldWithSize:16];
        [_nextBtn setTitleColor:[UIColor whiteFontColor] forState:UIControlStateNormal];
        [_nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
        [_nextBtn addTarget:self action:@selector(clickNext) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextBtn;
}

- (XLVerificationCodeButton *)vfCodeBtn {
    if (!_vfCodeBtn) {
        _vfCodeBtn = [XLVerificationCodeButton new];
        _vfCodeBtn.buttonDelegate = self;
    }
    return _vfCodeBtn;
}

- (XLImageView *)circleBgIG {
    if (!_circleBgIG) {
        _circleBgIG = [XLImageView new];
        _circleBgIG.image = XLIMAGE(@"tybg");
    }
    return _circleBgIG;
}

- (XLLabel *)checkLB {
    if (!_checkLB) {
        _checkLB = [XLLabel new];
        _checkLB.font = [UIFont fontOfPingFangSCSemiboldWithSize:15];
        _checkLB.textColor = [UIColor blackFontColor];
        _checkLB.text = @"验证码";
    }
    return _checkLB;
}

- (XLTextField *)checkTF {
    if (!_checkTF) {
        _checkTF = [XLTextField new];
        _checkTF.font = [UIFont fontOfPingFangSCRegularWithSize:16];
        _checkTF.placeholder = @"请输入验证码";
        _checkTF.tintColor = [UIColor redFontColor];
        _checkTF.textColor = [UIColor blackFontColor];
        _checkTF.enablesReturnKeyAutomatically = YES;
        _checkTF.returnKeyType = UIReturnKeyNext;
    }
    return _checkTF;
}

- (XLView *)checkLineView {
    if (!_checkLineView) {
        _checkLineView = [XLView new];
        _checkLineView.backgroundColor = [UIColor grayLineColor];
    }
    return _checkLineView;
}

- (XLImageView *)checkIG {
    if (!_checkIG) {
        _checkIG = [XLImageView new];
        _checkIG.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickFetchImageVerify)];
        [_checkIG addGestureRecognizer:tap];
    }
    return _checkIG;
}

- (XLButton *)selectBtn {
    if (!_selectBtn) {
        _selectBtn = [XLButton new];
        [_selectBtn setImage:XLIMAGE(@"check_n") forState:UIControlStateNormal];
        [_selectBtn setImage:XLIMAGE(@"check_s") forState:UIControlStateSelected];
        _selectBtn.selected = YES;
        [_selectBtn setCurrentButtonHotSize:CGSizeMake(40, 40)];
        [_selectBtn addTarget:self action:@selector(clickSelectProtocol:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectBtn;
}

- (YYLabel *)protocolLB {
    if (!_protocolLB) {
        _protocolLB = [YYLabel new];
        NSString *title = @"已阅读并同意《服务条款和隐私政策》";
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:title];
        [attri yy_setAttributes:@{NSFontAttributeName:[UIFont fontOfPingFangSCRegularWithSize:13], NSForegroundColorAttributeName:[UIColor blackFontColor]}];
        [attri yy_setTextHighlightRange:[title rangeOfString:@"《服务条款和隐私政策》"] color:[UIColor blueFontColor] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            XLLog(@"点击了协议");
            [self clickWatchProtocol];
        }];
        _protocolLB.attributedText = attri;
        _protocolLB.textAlignment = NSTextAlignmentCenter;
        
        CGFloat width = [title calculateRowYYLabelWidthWithAttri:attri height:20];
        _protocolLB.preferredMaxLayoutWidth = width;
    }
    return _protocolLB;
}
@end
